<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2018/1/15
 * Time: 下午7:38
 */
namespace Home\Controller;

use Think\Controller;
use GLOAB_CODE;

class NotebookController extends Controller
{
    /**
     * 新建策略文件夹
     */
    public function newDir()
    {
        $rst = array();
        if (isset($_POST['dirname'])) {
            $dirname = $_POST['dirname'];
            if (!empty($_SESSION['user_info']['id'])) {
                $uid = $_SESSION['user_info']['id'];
                $note = D('Dir');
                $flag = $note->createNoteDir(intval($uid), $dirname);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '新建失败';
                    $rst['flag'] = C('COMMON_ERROR');
                } elseif ($flag == -2) {
                    $rst['status'] = false;
                    $rst['info'] = '重复添加';
                    $rst['flag'] = C('COMMON_DUPLICATE_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '未登录';
                $rst['flag'] = C('UNLOGIN_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '参数不全';
            $rst['flag'] = C('PARAM_LESS_ERROR');
        }
        echo json_encode($rst);
    }

    /**
     * 重命名策略文件夹
     */
    public function renameDir()
    {
        $rst = array();
        if (isset($_POST['did']) && isset($_POST['dirname'])) {
            if (isset($_SESSION['user_info']['id'])) {
                $did = $_POST['did'];
                $dirname = $_POST['dirname'];
                $note = D('Dir');
                $flag = $note->renameNoteDir($did, $dirname);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '重命名成功';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '重命名失败';
                    $rst['flag'] = C('COMMON_ERROR');
                } elseif ($flag == -2) {
                    $rst['status'] = false;
                    $rst['info'] = '重命名冲突';
                    $rst['flag'] = C('COMMON_DUPLICATE_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '未登录';
                $rst['flag'] = C('UNLOGIN_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '参数不全';
            $rst['flag'] = C('PARAM_LESS_ERROR');
        }
        echo json_encode($rst);
    }

    /**
     * 删除策略文件夹
     */
    public function delDir()
    {
        $rst = array();
        if (isset($_POST['did'])) {
            if (isset($_SESSION['user_info']['id'])) {
                $did = intval($_POST['did']);
                $note = D('Dir');
                $flag = $note->delNoteDir($did);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '删除成功';
                } else {
                    $rst['status'] = false;
                    $rst['info'] = '删除失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '参数不全';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }

    /**
     * 新建策略文件
     */
    public function newFile()
    {
        $rst = array();
        if (isset($_POST['did'])) {
            $did = $_POST['did'];
            if (!empty($_SESSION['user_info']['id'])) {
                $uid = intval($_SESSION['user_info']['id']);
                $note = D('Dir');
                $flag = $note->createNoteFile($uid, $did);
                if ($flag != -1) {
                    $rst['status'] = true;
                    $rst['info'] = '新建成功';
                    $rst['data'] = $flag;
                } else {
                    $rst['status'] = false;
                    $rst['info'] = '新建失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '未登录';
                $rst['flag'] = C('UNLOGIN_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '参数不全';
            $rst['flag'] = C('PARAM_LESS_ERROR');
        }
        echo json_encode($rst);
    }

    /**
     * 重命名策略文件
     */
    public function renameFile()
    {
        $rst = array();
        if (isset($_POST['did']) && isset($_POST['cid']) && isset($_POST['cname'])) {
            if (!empty($_SESSION['user_info']['id'])) {
                $uid = intval($_SESSION['user_info']['id']);
                $did = intval($_POST['did']);
                $cid = intval($_POST['cid']);
                $cname = $_POST['cname'];
                $note = D('Dir');
                $flag = $note->renameNoteFile($uid, $did, $cid, $cname);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '重命名成功';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '重命名失败';
                    $rst['flag'] = C('COMMON_ERROR');
                } elseif ($flag == -2) {
                    $rst['status'] = false;
                    $rst['info'] = '重命名冲突';
                    $rst['flag'] = C('COMMON_DUPLICATE_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '未登录';
                $rst['flag'] = C('UNLOGIN_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '参数不全';
            $rst['flag'] = C('PARAM_LESS_ERROR');
        }
        echo json_encode($rst);
    }

    /**
     * 保存&&更新策略文件
     */
    public function saveFile()
    {
        $rst = array();
        if (isset($_POST['cid']) && isset($_POST['did']) && isset($_POST['code'])) {
            if (!empty($_SESSION['user_info']['id'])) {
                $model = D('Dir');
                $uid = intval($_SESSION['user_info']['id']);
                $did = intval($_POST['did']);
                $cid = intval($_POST['cid']);
                $code = $_POST['code'];
                $flag = $model->saveNoteFile($uid, $did, $cid, $code);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '保存成功';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '保存失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '未登录';
                $rst['flag'] = C('UNLOGIN_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '参数不全';
            $rst['flag'] = C('PARAM_LESS_ERROR');
        }
        echo json_encode($rst);
    }

    /**
     * 删除策略文件
     */
    public function delFile()
    {
        $rst = array();
        if (isset($_POST['did']) && isset($_POST['cid'])) {
            if (!empty($_SESSION['user_info']['id'])) {
                $uid = intval($_SESSION['user_info']['id']);
                $cid = intval($_POST['cid']);
                $did = intval($_POST['did']);
                $note = D('Dir');
                $flag = $note->delFile($uid, $did, $cid);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '删除成功';
                } else {
                    $rst['status'] = false;
                    $rst['info'] = '删除失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '未登录';
                $rst['flag'] = C('UNLOGIN_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '参数不全';
            $rst['flag'] = C('PARAM_LESS_ERROR');

        }
        echo json_encode($rst);
    }

    /**
     * 查询树状图列表数据
     */
    public function findAll()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            $note = D('Dir');
            $flag = $note->findall($uid);
            if ($flag != -1) {
                $rst['status'] = true;
                $rst['info'] = '查询成功';
                $rst['data'] = $flag;
            } else {
                $rst['status'] = false;
                $rst['info'] = '查询失败';
                $rst['flag'] = C('COMMON_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }
}

